package com.sgb.kjwl.view.ui.fragment.transport;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentOfferOwnerBinding;
import com.sgb.kjwl.model.entity.self.OrderQuoteEntity;
import com.sgb.kjwl.model.entity.transport.OwnerGoodsDetailInfo;
import com.sgb.kjwl.view.constant.RouterKey;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LogisticsEvent;
import com.sgb.kjwl.view.ui.activity.self.logistics.OrderOwnerDetailsActivity;
import com.sgb.kjwl.view.ui.adapter.self.logistics.OrderOwnerAdapter;
import com.sgb.kjwl.viewmodel.self.logistics.OrderOwnerFragmentModel;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Time:2021/8/27
 * Author:黄淋
 * Description:
 */
public class OrderOwnerFragment extends AppLazyFragment {
    FragmentOfferOwnerBinding binding;
    OrderOwnerFragmentModel viewModel;
    //报价列表数据适配器
    private OrderOwnerAdapter orderOwnerAdapter;
    OwnerGoodsDetailInfo sourceDetails;

    public static OrderOwnerFragment newInstance(OwnerGoodsDetailInfo detailInfo) {
        OrderOwnerFragment fragment = new OrderOwnerFragment();
        Bundle args = new Bundle();
        args.putSerializable(RouterKey.DATA, detailInfo);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_offer_owner, container, false);
        binding.setLifecycleOwner(this);
        viewModel = new ViewModelProvider(this).get(OrderOwnerFragmentModel.class);
        return binding.getRoot();
    }

    @Override
    protected void onLazyLoad() {
        viewModel.getOrderOwnerList(getContext(), false, sourceDetails);
    }

    @Override
    protected void initView() {
        sourceDetails = (OwnerGoodsDetailInfo) getArguments().getSerializable(RouterKey.DATA);
        initAdapter();
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
    }

    private void initAdapter() {
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.shape_list_divide));
        LinearLayoutManager layoutManager1 = new LinearLayoutManager(getContext());
        //首个列表页列表初始化
        if (orderOwnerAdapter == null) {
            orderOwnerAdapter = new OrderOwnerAdapter(getContext(), null);
        }
        binding.recyclerView.addItemDecoration(divider);
        binding.recyclerView.setLayoutManager(layoutManager1);
        binding.recyclerView.setAdapter(orderOwnerAdapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.isMore.set(false);
                viewModel.getOrderOwnerList(getContext(), false, sourceDetails);
            }

            @Override
            public void onLoadMore() {
                viewModel.isMore.set(true);
                viewModel.getOrderOwnerList(getContext(), true, sourceDetails);
            }
        });
        orderOwnerAdapter.setOnItemClick(new AdapterOnItemClick<OrderQuoteEntity>() {
            @Override
            public void onItemClick(OrderQuoteEntity entity2, int position) {
                OrderOwnerDetailsActivity.start(getActivity(), entity2.getGoodsOfferNo());
            }
        });
        orderOwnerAdapter.setonEditClick(entity -> viewModel.agreeOffer(getContext(), entity.getGoodsOfferNo(), entity.getSourceNo()));

        viewModel.ldListOrder.observe(this, list -> showOrderOwner(list, viewModel.isMore.get()));
        viewModel.agreeOfferRefresh.observe(this, a -> agreeOfferRefresh());
    }

    /**
     * 刷新接单数据显示
     *
     * @param list
     * @param isMore
     */
    public void showOrderOwner(List<OrderQuoteEntity> list, boolean isMore) {
        if (list == null) {
            return;
        }
        if (isMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() != 0) {
                orderOwnerAdapter.addDatas(list);
            }
        } else {
            binding.recyclerView.refreshComplete();
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.recyclerView.setVisibility(View.GONE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
                binding.recyclerView.setVisibility(View.VISIBLE);
            }
            orderOwnerAdapter.setDatas(list);
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LogisticsEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_REFRESH_ORDER_OWNER_PAGE) {
            agreeOfferRefresh();
        }
    }

    /**
     * 货主同意了某条报价信息，需要刷新页面状态
     */
    public void agreeOfferRefresh() {
        if (sourceDetails != null) {
            viewModel.isMore.set(false);
            viewModel.getOrderOwnerList(getContext(), false, sourceDetails);
            EventBus.getDefault().post(LogisticsEvent.make(EventConstant.EVENT_GOODS_AGREED_OFFER, null));
        }
    }

}
